<html>
<head><meta charset="utf-8"><title>Avoiding unwrap in an Option pattern · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html">Avoiding unwrap in an Option pattern</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="197060603"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197060603" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197060603">(May 10 2020 at 15:15)</a>:</h4>
<p>Is there an unwrap-free way to write this code?</p>
<div class="codehilite"><pre><span></span><code><span class="n">an_option</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">compute_thing</span><span class="p">()</span><span class="o">?</span><span class="p">);</span><span class="w"></span>
<span class="n">an_option</span><span class="p">.</span><span class="n">as_ref</span><span class="p">().</span><span class="n">unwrap</span><span class="p">()</span><span class="w"></span>
</code></pre></div>



<a name="197060634"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197060634" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197060634">(May 10 2020 at 15:16)</a>:</h4>
<p>Semantically, "put a value into an <code>Option</code> and give me a reference to that value".</p>



<a name="197060796"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197060796" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197060796">(May 10 2020 at 15:18)</a>:</h4>
<p><span class="user-mention" data-user-id="239881">@Josh Triplett</span> <a href="https://doc.rust-lang.org/nightly/std/option/enum.Option.html#method.get_or_insert" title="https://doc.rust-lang.org/nightly/std/option/enum.Option.html#method.get_or_insert">https://doc.rust-lang.org/nightly/std/option/enum.Option.html#method.get_or_insert</a> I think?</p>



<a name="197060860"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197060860" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197060860">(May 10 2020 at 15:19)</a>:</h4>
<p>but not in the language -- that has to use unreachable_unchecked to avoid an unwrap <a href="https://doc.rust-lang.org/nightly/src/core/option.rs.html#865" title="https://doc.rust-lang.org/nightly/src/core/option.rs.html#865">https://doc.rust-lang.org/nightly/src/core/option.rs.html#865</a></p>



<a name="197060864"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197060864" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197060864">(May 10 2020 at 15:19)</a>:</h4>
<p>That's a really nice problem <span aria-label="heart" class="emoji emoji-2764" role="img" title="heart">:heart:</span> I don't think so as the value has to be moved into <code>Some</code>, during which it can't be referenced</p>



<a name="197060969"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197060969" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197060969">(May 10 2020 at 15:21)</a>:</h4>
<p><span class="user-mention" data-user-id="116122">@simulacrum</span> Interesting. Wouldn't have thought of using <code>get_or_insert</code> for a case where I <em>know</em> the value is <code>None</code>.</p>



<a name="197060978"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197060978" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197060978">(May 10 2020 at 15:21)</a>:</h4>
<p>Thanks, that should work.</p>



<a name="197061028"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197061028" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197061028">(May 10 2020 at 15:22)</a>:</h4>
<p><code>None.get_or_insert(value)</code> looks really weird <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span></p>



<a name="197061031"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197061031" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197061031">(May 10 2020 at 15:22)</a>:</h4>
<p>We <em>could</em> expose a <code>set(&amp;mut self, value: T) -&gt; &amp;mut T</code> API as well which would avoid the initial branch, but I doubt you care</p>



<a name="197061067"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197061067" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197061067">(May 10 2020 at 15:23)</a>:</h4>
<p><span class="user-mention" data-user-id="116122">@simulacrum</span> 1) I really don't, and 2) in an ideal world, rust can just inline <code>get_or_insert</code> and notice statically that the value must be None, and thus avoid the branch.</p>



<a name="197061104"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197061104" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197061104">(May 10 2020 at 15:23)</a>:</h4>
<p>ah, well, presuming that's statically known then yes</p>



<a name="197061121"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197061121" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197061121">(May 10 2020 at 15:23)</a>:</h4>
<p>(generally my code where I "know" such things it's because there's code far away that "knows" them)</p>



<a name="197061269"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197061269" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197061269">(May 10 2020 at 15:25)</a>:</h4>
<p>It does look like in the trivial case at least we do skip the branch <a href="https://rust.godbolt.org/z/hEAyqw" title="https://rust.godbolt.org/z/hEAyqw">https://rust.godbolt.org/z/hEAyqw</a></p>



<a name="197061271"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197061271" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197061271">(May 10 2020 at 15:25)</a>:</h4>
<p>The pattern here is "get thing out of LRU cache if it's there, if not, go get the uncached thing expensively, either way I need a reference, then later I want to stuff any uncached thing into the LRU cache".</p>



<a name="197061354"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197061354" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197061354">(May 10 2020 at 15:26)</a>:</h4>
<p>So <code>an_option</code> here is <code>uncached_thing</code>, which I've just created, and which contains <code>None</code> unless I had to go expensively retrieve the thing.</p>



<a name="197061356"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197061356" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197061356">(May 10 2020 at 15:26)</a>:</h4>
<p>makes sense</p>



<a name="197061379"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197061379" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197061379">(May 10 2020 at 15:27)</a>:</h4>
<p>Also, thanks for testing the optimization there. I'm always impressed with the degree to which Rust makes sensible code patterns work well without having to make them less sensible.</p>



<a name="197061491"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197061491" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197061491">(May 10 2020 at 15:28)</a>:</h4>
<p>fwiw it looks like the as_ref().unwrap() also optimizes away to same assembly</p>



<a name="197061576"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197061576" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197061576">(May 10 2020 at 15:29)</a>:</h4>
<p>I had little doubt that it would, but I don't like writing <code>unwrap</code> if I don't absolutely have to.</p>



<a name="197061597"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197061597" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197061597">(May 10 2020 at 15:29)</a>:</h4>
<p>As far as I'm concerned, <code>unwrap</code> has a proof obligation just like <code>unsafe</code> does.</p>



<a name="197061643"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197061643" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197061643">(May 10 2020 at 15:30)</a>:</h4>
<p>yeah, totally can understand that :)</p>



<a name="197061650"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197061650" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197061650">(May 10 2020 at 15:30)</a>:</h4>
<p>Thanks for the help! I'm going to go with the <code>get_or_insert</code> solution.</p>



<a name="197061664"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197061664" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197061664">(May 10 2020 at 15:31)</a>:</h4>
<p>It helps that I can avoid <code>get_or_insert_with</code> because I know the function would <em>always</em> be called.</p>



<a name="197061665"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197061665" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197061665">(May 10 2020 at 15:31)</a>:</h4>
<p>I sort of wish that we had a way to express this in the language itself -- essentially getting access to the "slot" of <code>Some(T)</code> after storing into it -- but there's no nice way to express that even syntax wise</p>



<a name="197061721"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197061721" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197061721">(May 10 2020 at 15:32)</a>:</h4>
<p><span class="user-mention" data-user-id="116122">@simulacrum</span> I know what you mean, and I was thinking the same thing when I posted this question. Hard to articulate what it <em>means</em>, but there's an intuitive notion.</p>



<a name="197061791"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197061791" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197061791">(May 10 2020 at 15:33)</a>:</h4>
<p>I guess <code>&amp;mut T</code> is pretty close to "give me access to the "slot" of a <code>Some</code>."</p>



<a name="197061800"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197061800" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197061800">(May 10 2020 at 15:33)</a>:</h4>
<p>An exclusive reference to a location.</p>



<a name="197061916"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197061916" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197061916">(May 10 2020 at 15:34)</a>:</h4>
<p>Yes, I've contemplated some syntax to give you <code>&amp;mut uninit T</code> basically to an Option's Some slot such that you can write to it and then the Option becomes Some and you can just &amp;* your prior reference to get a &amp;T to the slot</p>



<a name="197061938"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197061938" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197061938">(May 10 2020 at 15:35)</a>:</h4>
<p>but there's no way today to get a pointer to the Some slot of an Option without actually, well, making it Some and doing a match or w/e</p>



<a name="197061941"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197061941" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197061941">(May 10 2020 at 15:35)</a>:</h4>
<p>That's interesting!</p>



<a name="197061998"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197061998" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197061998">(May 10 2020 at 15:36)</a>:</h4>
<p>this is very specifically tailored to option, in some sense, and it gets into the enum-variants-as-types rfc(s), too</p>



<a name="197062004"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197062004" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197062004">(May 10 2020 at 15:36)</a>:</h4>
<p>In theory, we could easily add this function to <code>Option&lt;T&gt;</code>: <code>fn (&amp;mut self) -&gt; &amp;mut MaybeUninit&lt;T&gt;</code>.</p>



<a name="197062018"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197062018" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197062018">(May 10 2020 at 15:37)</a>:</h4>
<p>yes, but that doesn't do what you need it to -- the key bit is that initialization would <em>also</em> write the enum tag bit/byte</p>



<a name="197062025"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197062025" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197062025">(May 10 2020 at 15:37)</a>:</h4>
<p>Ah. Such that if you <em>don't</em> write to the value, it isn't a <code>Some</code>?</p>



<a name="197062034"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197062034" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197062034">(May 10 2020 at 15:37)</a>:</h4>
<p>right, yes</p>



<a name="197062043"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197062043" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197062043">(May 10 2020 at 15:37)</a>:</h4>
<p>Now that's fascinating.</p>



<a name="197062101"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197062101" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197062101">(May 10 2020 at 15:38)</a>:</h4>
<p>How would that even work? That would mean that it isn't an ordinary "place", because assigning to it would also have a side effect.</p>



<a name="197062119"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197062119" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197062119">(May 10 2020 at 15:38)</a>:</h4>
<p>it's basically something like "DerefMut" but with a "destructor" on the returned mut</p>



<a name="197062173"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197062173" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197062173">(May 10 2020 at 15:39)</a>:</h4>
<p>"destructor", or "constructor"? It takes effect when given a value, right?</p>



<a name="197062191"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197062191" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197062191">(May 10 2020 at 15:39)</a>:</h4>
<p>well, perhaps, or perhaps when the &amp;mut T ends</p>



<a name="197062264"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197062264" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197062264">(May 10 2020 at 15:40)</a>:</h4>
<p>(I guess it wouldn't really be possible to observe which of those it is)</p>



<a name="197062281"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197062281" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197062281">(May 10 2020 at 15:40)</a>:</h4>
<p>in some sense you can get this today with DerefMut if your option has a default value you're willing to let it take on if you fail to actually write</p>



<a name="197062289"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197062289" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197062289">(May 10 2020 at 15:41)</a>:</h4>
<p>e.g. we could do <code>DerefMut for Option</code> that returned &amp;mut T and initialized to T::default() prior to returning that ref</p>



<a name="197062340"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197062340" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197062340">(May 10 2020 at 15:42)</a>:</h4>
<p>if we had such "guaranteed write" references we'd also be able to use them I think with some work for Read's uninitialized buffers problem</p>



<a name="197062361"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197062361" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197062361">(May 10 2020 at 15:42)</a>:</h4>
<p>but I have no sense of how we'd actually encode what they <em>meant</em></p>



<a name="197062371"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197062371" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197062371">(May 10 2020 at 15:43)</a>:</h4>
<p>i.e. what conditions  they impose -- just saying "you must write to this" seems easy but what that actually means in a more spec-y way I haven't been able to come up with</p>



<a name="197062445"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197062445" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197062445">(May 10 2020 at 15:44)</a>:</h4>
<p>A "guaranteed write" reference would have similar obligations to an uninitialized <code>let</code>?</p>



<a name="197062460"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197062460" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197062460">(May 10 2020 at 15:45)</a>:</h4>
<p>"the compiler must statically know you write to this before you read it"?</p>



<a name="197062461"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197062461" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197062461">(May 10 2020 at 15:45)</a>:</h4>
<p>kind of, but it's harder in the sense that you probably want to be able to pass it to some function</p>



<a name="197062471"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197062471" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197062471">(May 10 2020 at 15:45)</a>:</h4>
<p>you can't actually do <em>anything</em> pretty much with an uninitalized let</p>



<a name="197062479"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197062479" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197062479">(May 10 2020 at 15:45)</a>:</h4>
<p>whereas here you want to be able to carry that proof obligation downwards for a bit</p>



<a name="197062624"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197062624" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197062624">(May 10 2020 at 15:49)</a>:</h4>
<p>Well, in the meantime, hyperfine thanks you for your incremental contribution to performance. :)</p>



<a name="197062647"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197062647" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197062647">(May 10 2020 at 15:50)</a>:</h4>
<p>Not having to pop a value out of the LRU cache and put it back later seems to have saved a few tens of milliseconds over my standard six-second run.</p>



<a name="197062856"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197062856" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197062856">(May 10 2020 at 15:54)</a>:</h4>
<p>It's fun doing performance optimization on software that runs operations millions of times. Tiny tweaks give huge wins.</p>



<a name="197063162"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197063162" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197063162">(May 10 2020 at 16:01)</a>:</h4>
<p>On a separate note, I'm <em>eagerly</em> awaiting the ability to write <code>if condition &amp;&amp; let Some(x) = y {</code>.</p>



<a name="197063226"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197063226" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197063226">(May 10 2020 at 16:03)</a>:</h4>
<p>Because right now I have to write:</p>
<div class="codehilite"><pre><span></span><code><span class="k">if</span><span class="w"> </span><span class="n">condition</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">cached_thing</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">thing_cache</span><span class="p">.</span><span class="n">get</span><span class="p">(</span><span class="o">&amp;</span><span class="n">key</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">cached_thing</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">uncached_thing</span><span class="p">.</span><span class="n">get_or_insert</span><span class="p">(</span><span class="n">complex_get_thing</span><span class="p">()</span><span class="o">?</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">uncached_thing</span><span class="p">.</span><span class="n">get_or_insert</span><span class="p">(</span><span class="n">complex_get_thing</span><span class="p">()</span><span class="o">?</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="197066145"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Avoiding%20unwrap%20in%20an%20Option%20pattern/near/197066145" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mark Drobnak <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Avoiding.20unwrap.20in.20an.20Option.20pattern.html#197066145">(May 10 2020 at 17:03)</a>:</h4>
<p>You can simplify this with a match (though not sure if it's important that the condition is evaluated before the option):</p>
<div class="codehilite"><pre><span></span><code><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">condition</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">true</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">option</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span><span class="w"></span>

<span class="w">    </span><span class="k">match</span><span class="w"> </span><span class="n">option</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nb">Some</span><span class="p">(</span><span class="n">value</span><span class="p">)</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">condition</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;Some &amp;&amp; condition&quot;</span><span class="p">),</span><span class="w"></span>
<span class="w">        </span><span class="n">_</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;None || !condition&quot;</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=2a10d5ef9a4495efcc1c3433eef2eb09" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=2a10d5ef9a4495efcc1c3433eef2eb09">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=2a10d5ef9a4495efcc1c3433eef2eb09</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>